package java复习202407;

import org.junit.jupiter.api.Test;

import java.util.Comparator;

public class c8_比较器验证 {

    class User implements Comparable<User>{
        private int age;

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public int compareTo(User user) {
            return this.getAge() - user.getAge();
        }
    }

    @Test
    public void comparable验证(){
        System.out.println(new Integer(1).compareTo(2));

        User user1 = new User();
        User user2 = new User();
        user1.setAge(1);
        user2.setAge(3);
        int i = user1.compareTo(user2);
        System.out.println(i);
    }


    class 自定义比较器 implements Comparator<User>{
        @Override
        public int compare(User o1, User o2) {
            return o1.getAge() - o2.getAge();
        }
    }

    @Test
    public void comparator验证(){

        User user1 = new User();
        User user2 = new User();
        user1.setAge(1);
        user2.setAge(3);

        System.out.println(new 自定义比较器().compare(user1,user2));

    }


}
